package com.zhuangfei.hpuhelper.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.OptionsPickerView;
import com.hpu.bean.Course;
import com.hpu.bean.CourseListEntity;
import com.hpu.bean.Electric;
import com.hpu.bean.ElectricSummary;
import com.hpu.bean.Week;
import com.hpu.constants.HttpConstants;
import com.hpu.constants.ShareConstants;
import com.hpu.parser.CourseParser;
import com.hpu.parser.ElectricParser;
import com.hpu.parser.WeekParser;
import com.hpu.service.CourseService;
import com.hpu.service.ElectricService;
import com.hpu.service.LoginService;
import com.hpu.service.WeekService;
import com.hpu.tools.ShareTools;
import com.hpu.tools.TimeTools;
import com.zhuangfei.generalpage.BaseActivity;
import com.zhuangfei.hpuhelper.bean.ElectricCharge;
import com.zhuangfei.hpuhelper.tools.ActivityTools;
import com.zhuangfei.hpuhelper.tools.CourseTools;
import com.zhuangfei.hpuhelper.tools.DefaultHandler;
import com.zhuangfei.hpuhelper.tools.ElectricChargeTools;
import com.zhuangfei.hpuhelper.tools.ReflectTools;
import com.zhuangfei.hpuhelper.view.CourseDetailActivity;
import com.zhuangfei.hpuhelper.view.CourseManagerActivity;
import com.zhuangfei.hpuhelper.view.MenuActivity;
import com.zhuangfei.hpuhelper.view.MyApplication;
import com.zhuangfei.hpuhelper.view.NoTimeClassActivity;
import com.zhuangfei.timetable.core.OnSubjectBindViewListener;
import com.zhuangfei.timetable.core.OnSubjectItemClickListener;
import com.zhuangfei.timetable.core.SubjectBean;
import com.zhuangfei.timetable.core.TimetableView;
import com.zhuangfei.toolkit.IntentToolkit;
import com.zhuangfei.toolkit.tools.ToastTools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.lzf.serviceplatform.R;

/**
 * @author Administrator 刘壮飞
 * 
 */
public class ElectricActivity extends BaseActivity{

	private LoginService loginService;
	ElectricService electricService;

	String build="";

	@BindView(R.id.id_electric_build)
	TextView buildTextView;

	List<Map<String,String>> electricList=new ArrayList<>();
	@BindView(R.id.id_electric_listview)
	ListView listView;
	SimpleAdapter simpleAdapter;

	ArrayList<String> optionsItems;
	ArrayList<ArrayList<String>> options2Items;
	ArrayList<ArrayList<ArrayList<String>>> options3Items;

	List<ElectricCharge> charges;
	List<String> floors;
	List<String> rooms;

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_electric);
		ActivityTools.setTransparent(this);
		ButterKnife.bind(this);
		initView();
		initData();
	}

	private void initData() {
		charges=ElectricChargeTools.load(this,"electric.txt");
		if(charges==null) charges=new ArrayList<>();

		floors=new ArrayList<>();
		for(int i=1;i<7;i++){
			floors.add("0"+i);
		}

		rooms=new ArrayList<>();
		for(int i=1;i<56;i++){
			if(i<10){
				rooms.add("0"+i);
			}else{
				rooms.add(i+"");
			}
		}

		optionsItems = new ArrayList<>();
		options2Items = new ArrayList<>();
		options3Items = new ArrayList<>();
		for (int i = 0; i < charges.size(); i++) {
			ElectricCharge charge=charges.get(i);
			if(TextUtils.isEmpty(charge.getBuildName())) continue;

			optionsItems.add(charge.getBuildName());
			ArrayList<String> optionItems2_01 = new ArrayList<>();
			ArrayList<ArrayList<String>> optionItems3_01 = new ArrayList<>();

			for (int j = 0; j < floors.size(); j++) {
				String floor=floors.get(j);
				if(TextUtils.isEmpty(floor)) continue;
				optionItems2_01.add(floor);
				ArrayList<String> optionItems3_01_01 = new ArrayList<>();
				for (int m = 0; m < rooms.size(); m++) {
					String room=rooms.get(m);
					if(TextUtils.isEmpty(room)) continue;
					optionItems3_01_01.add(room);
				}
				optionItems3_01.add(optionItems3_01_01);
			}
			options2Items.add(optionItems2_01);
			options3Items.add(optionItems3_01);
		}
	}

	private void initView() {
		getViewHelper().showSuccessView();
		electricService=new ElectricService(handler,this);
		loginService = new LoginService(this,handler);

		simpleAdapter=new SimpleAdapter(this,electricList,R.layout.item_electric,
				new String[]{"time","money"},
				new int[]{R.id.id_item_electric_time,R.id.id_item_electric_money});
		listView.setAdapter(simpleAdapter);
	}

	DefaultHandler handler = new DefaultHandler(MyApplication.getInstance()) {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			getViewHelper().showSuccessView();

			switch (msg.what) {
			case HttpConstants.QUERY_ELECTRIC_SUCCESS:
				ElectricParser parser=new ElectricParser();
				ElectricSummary summary=parser.parse(msg.obj.toString());
				showResult(summary);
				break;

			case HttpConstants.VPN_TOOLONG:
				if(handler.isIgnoreVpnExpired()){
					break;
				}

				getViewHelper().showLoadingView();
				loginService.login(ShareTools.get(ElectricActivity.this, ShareConstants.STORE_USER, "").toString(),
						ShareTools.get(ElectricActivity.this, ShareConstants.STORE_PASSWORD1, "").toString(),
						ShareTools.get(ElectricActivity.this, ShareConstants.STORE_PASSWORD2, "").toString());
				break;

			// 登录成功
			case HttpConstants.LOGIN_SUCCESS:
				ShareTools.put(ElectricActivity.this, ShareConstants.STORE_LOGINCOOKIE, (String) msg.obj);
				electricService.getElectric(build);
				break;
			}
		};
	};

	private void showResult(ElectricSummary summary) {
		if(summary==null) return;;
		buildTextView.append(",剩余:"+summary.getSurplus()+"°");
		List<Electric> electricList=summary.getElectricList();
		if(electricList!=null){
			for(Electric electric:electricList){
				Map<String,String> map=new HashMap<>();
				map.put("time",electric.getTime());
				map.put("money",electric.getMoney()+"°");
				this.electricList.add(map);
			}
		}
		simpleAdapter.notifyDataSetChanged();
	}

	@OnClick(R.id.id_electric_choosebuild)
	public void chooseBuild(){
		//条件选择器
		OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
			@Override
			public void onOptionsSelect(int options1, int option2, int options3, View v) {
				//返回的分别是三个级别的选中位置
				String tx = optionsItems.get(options1)
						+ ","+(option2+1)+options3Items.get(options1).get(option2).get(options3);
				build = charges.get(options1).getBuildNumber()+options2Items.get(options1).get(option2)
						+options3Items.get(options1).get(option2).get(options3);

				buildTextView.setText(tx);
				getViewHelper().showLoadingView();
				electricList.clear();
				electricService.getElectric(build);
			}
		}).setSubmitText("确定")//确定按钮文字
				.setCancelText("取消")//取消按钮文字
				.setTitleText("选择寝室")//标题
				.setLabels("", "层", "室")//设置选择的三级单位
				.setLinkage(false)
				.build();
		pvOptions.setPicker(optionsItems, options2Items, options3Items);
		pvOptions.show();
	}

	@OnClick(R.id.id_back)
	public void goBack(){
		new IntentToolkit().with(this)
				.target(MenuActivity.class)
				.canBack()
				.execute();
	}

	@Override
	public void onBackPressed() {
		goBack();
	}
}
